home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / Found / FWCommon / SLPriStr.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  5.7 KB  |  245 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                SLPriStr.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFound.hpp"
  11.  
  12. #ifndef SLPRIDEB_H
  13. #include "SLPriDeb.h"
  14. #endif
  15.  
  16. #ifndef SLPRISTR_H
  17. #include "SLPriStr.h"
  18. #endif
  19.  
  20. #ifndef SLPRIMEM_H
  21. #include "SLPriMem.h"
  22. #endif
  23.  
  24. #ifdef FW_BUILD_MAC
  25. #include <Memory.h>
  26. #endif
  27.  
  28. #ifdef FW_BUILD_MAC
  29. #pragma segment FWCommon
  30. #endif
  31.  
  32. #ifndef FW_USE_STANDARD_LIBRARY
  33. #define FW_USE_STANDARD_LIBRARY 1
  34. #endif
  35.  
  36. #if FW_USE_STANDARD_LIBRARY
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <ctype.h>
  40. #endif
  41.  
  42. //----------------------------------------------------------------------------------------
  43. // FW_PrimitiveStringLength
  44. //----------------------------------------------------------------------------------------
  45.  
  46. size_t FW_PrimitiveStringLength(const char * p)
  47. {
  48.     // No try block necessary - Do not throw
  49.     FW_PRIV_ASSERT(p != 0);
  50. #if FW_USE_STANDARD_LIBRARY
  51.     return strlen(p);
  52. #else
  53.     size_t i = 0;
  54.     for (; *p; p++, i++) ;
  55.     return i;
  56. #endif
  57. }
  58.  
  59. //----------------------------------------------------------------------------------------
  60. // FW_PrimitiveStringEqual
  61. //----------------------------------------------------------------------------------------
  62.  
  63. FW_Boolean FW_PrimitiveStringEqual(const char *p1, const char *p2)
  64. {
  65.     // No try block necessary - Do not throw
  66. #if FW_USE_STANDARD_LIBRARY
  67.     return strcmp(p1,p2)==0;
  68. #else
  69.     while (true)
  70.     {
  71.         char c1 = *p1++;
  72.         char c2 = *p2++;
  73.         
  74.         if (c1 != c2)
  75.             return false;
  76.         
  77.         if (!c1)
  78.             return true;
  79.     }
  80. #endif
  81. }
  82.  
  83. //----------------------------------------------------------------------------------------
  84. // FW_PrimitiveStringEqual
  85. //----------------------------------------------------------------------------------------
  86.  
  87. int FW_PrimitiveStringCompare(const char *p1, const char *p2)
  88. {
  89.     // No try block necessary - Do not throw
  90. #if FW_USE_STANDARD_LIBRARY
  91.     return strcmp(p1,p2);
  92. #else
  93.     int result = 0; // assume strings are equal
  94.     
  95.     const unsigned char* s1 = (const unsigned char*) p1;
  96.     const unsigned char* s2 = (const unsigned char*) p2;
  97.     
  98.     unsigned char c1 = 0;
  99.     unsigned char c2 = 0;
  100.  
  101.     while (true)
  102.     {
  103.         c1 = *s1++;
  104.         c2 = *s2++;
  105.         if (!c1)
  106.             break;
  107.         if (c1 != c2)
  108.             break;
  109.     }
  110.     
  111.     if (c1 < c2)
  112.         result = -1;
  113.     if (c1 > c2)
  114.         result = 1;
  115.         
  116.     return result;
  117. #endif
  118. }
  119.  
  120. //----------------------------------------------------------------------------------------
  121. // FW_PrimitiveStringCopy
  122. //----------------------------------------------------------------------------------------
  123.  
  124. void FW_PrimitiveStringCopy(const char *source, char *destination)
  125. {
  126.     // No try block necessary - Do not throw
  127. #if FW_USE_STANDARD_LIBRARY
  128.     strcpy(destination, source);
  129. #else
  130.     char c;
  131.     
  132.     while ((c = *source++) != '\0')
  133.         *destination++ = c;
  134.     
  135.     *destination = '\0';
  136. #endif
  137. }
  138.  
  139. //----------------------------------------------------------------------------------------
  140. // FW_PrimitiveStringDuplicate
  141. //----------------------------------------------------------------------------------------
  142. // Added by HLX
  143.  
  144. char* FW_PrimitiveStringDuplicate(const char *source)
  145. {
  146.     // No try block necessary - Do not throw
  147.     char* destination = (char*)FW_PrimitiveAllocateBlock(FW_PrimitiveStringLength(source) + 1);
  148.     if (destination)
  149.         FW_PrimitiveStringCopy(source, destination);
  150.     return destination;
  151. }
  152.  
  153. //----------------------------------------------------------------------------------------
  154. // FW_PrimitiveStringCatenate
  155. //----------------------------------------------------------------------------------------
  156.  
  157. void FW_PrimitiveStringCatenate(const char *source, char *destination)
  158. {
  159.     // No try block necessary - Do not throw
  160. #if FW_USE_STANDARD_LIBRARY
  161.     strcat(destination, source);
  162. #else
  163.     size_t len = FW_PrimitiveStringLength(destination);
  164.     FW_PrimitiveStringCopy(source, destination+len);
  165. #endif
  166. }
  167.  
  168. //----------------------------------------------------------------------------------------
  169. // FW_PrimitiveStringFindCharacter
  170. //----------------------------------------------------------------------------------------
  171.  
  172. char * FW_PrimitiveStringFindCharacter(const char *source, char c)
  173. {
  174.     // No try block necessary - Do not throw
  175. #if FW_USE_STANDARD_LIBRARY
  176.     return strchr(source, c);
  177. #else
  178.     while (1)
  179.     {
  180.         char cc;
  181.         if (c == (cc = *source))
  182.         {
  183.             return (char *) source;
  184.         }
  185.         else if (!cc)
  186.         {
  187.             return NULL;
  188.         }
  189.         else
  190.             source++;
  191.     }
  192. #endif
  193. }
  194.  
  195. //----------------------------------------------------------------------------------------
  196. // FW_PrimitiveCharacterToUpper
  197. //----------------------------------------------------------------------------------------
  198.  
  199. char FW_PrimitiveCharacterToUpper(char c)
  200. {
  201.     // No try block necessary - Do not throw
  202. #if FW_USE_STANDARD_LIBRARY
  203.     return toupper(c);
  204. #else
  205.     if (c >= 'a' && c <= 'z')
  206.         return (c - 'a' + 'A');
  207.     else
  208.         return (c);
  209. #endif
  210. }
  211.  
  212.  
  213. //----------------------------------------------------------------------------------------
  214. // FW_PrimitiveCharacterToLower
  215. //----------------------------------------------------------------------------------------
  216.  
  217. char FW_PrimitiveCharacterToLower(char c)
  218. {
  219.     // No try block necessary - Do not throw
  220. #if FW_USE_STANDARD_LIBRARY
  221.     return tolower(c);
  222. #else
  223.     if (c >= 'A' && c <= 'Z')
  224.         return (c + 'a' - 'A');
  225.     else
  226.         return (c);
  227. #endif
  228. }
  229.  
  230. //----------------------------------------------------------------------------------------
  231. // FW_PrimitiveCharacterIsSpace
  232. //----------------------------------------------------------------------------------------
  233.  
  234. char FW_PrimitiveCharacterIsSpace(char c)
  235. {
  236.     // No try block necessary - Do not throw
  237. #if FW_USE_STANDARD_LIBRARY
  238.     return isspace(c);
  239. #else
  240.     return ((c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f') ? c : 0);
  241. #endif
  242. }
  243.  
  244.  
  245.